package com.infoauto.opc.gateway.controller;

import com.infoauto.opc.gateway.entity.Result;
import com.infoauto.opc.gateway.entity.ResultCode;
import com.infoauto.opc.gateway.model.gtm4.CheckTheNumberOfUnits1;
import com.infoauto.opc.gateway.model.gtm4.CheckTheNumberOfUnits2;
import com.infoauto.opc.gateway.service.CheckTheNumberOfUnits2Service;
import com.sun.deploy.net.URLEncoder;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.RegionUtil;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 检查通过台数2
 */

@CrossOrigin
@RestController
@RequestMapping("/checkTheNumberOfUnits2")
public class CheckTheNumberOfUnits2Controller {
    @Autowired
    private CheckTheNumberOfUnits2Service service;

    @RequestMapping(value="/list" ,method= RequestMethod.GET)
    public Result selectByProducTionTimeAndShift(String productionTime,Integer shift){
        try{
            List<CheckTheNumberOfUnits2> list = service.selectByProducTionTimeAndShift(productionTime,shift);
            return new Result(ResultCode.SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
   }

    /**
     * 修改检查通过台数1
     */
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public Result update(@RequestBody CheckTheNumberOfUnits2 checkTheNumberOfUnits2) {
        //2.调用service更新
        service.update(checkTheNumberOfUnits2);
        return new Result(ResultCode.SUCCESS);
    }
    @RequestMapping(value = "/updateRemark", method = RequestMethod.PUT)
    public Result updateRemark(String id,String remark) {
        CheckTheNumberOfUnits2 checkTheNumberOfUnits2 = service.selectById(id);
        if(checkTheNumberOfUnits2!=null){
            checkTheNumberOfUnits2.setRemark(remark);
            checkTheNumberOfUnits2.setModifyDateTime(new Date());
            //2.调用service更新
            service.update(checkTheNumberOfUnits2);
        }
        return new Result(ResultCode.SUCCESS);
    }

    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public void export(HttpServletResponse response, String productionTime, Integer shift) throws Exception {
        //1.构造数据
        List<CheckTheNumberOfUnits2> list = service.selectByProducTionTimeAndShift(productionTime,shift);
        //2.创建工作簿
        XSSFWorkbook workbook = new XSSFWorkbook();
        String[] head1 = {"GTMC#4"};
        //3.构造sheet
        Sheet sheet = workbook.createSheet("检查通过台数2");
        //创建单元格样式对象
        CellStyle cellStyle = workbook.createCellStyle();
        //设置居中显示
        cellStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
        cellStyle.setBorderBottom(BorderStyle.MEDIUM);//下边框
        cellStyle.setBorderTop(BorderStyle.MEDIUM);//上边框
        cellStyle.setBorderLeft(BorderStyle.MEDIUM);//左边框
        cellStyle.setBorderRight(BorderStyle.MEDIUM);//有边框
        //设置单元格样式
        //设置边框
        //创建单元格样式对象
        CellStyle cellStyle1 = workbook.createCellStyle();
        cellStyle1.setBorderBottom(BorderStyle.NONE);//下边框
        cellStyle1.setBorderTop(BorderStyle.NONE);//上边框
        //设置字体
        Font font = workbook.createFont();//创建字体对象
        font.setFontName("宋体");//设置字体
        font.setFontHeightInPoints((short)16);//设置字号
        cellStyle1.setFont(font);
        Row row =sheet.createRow(0);
        AtomicInteger headersAi = new AtomicInteger();
        for (String title : head1) {
            Cell cell = row.createCell(headersAi.getAndIncrement());
            cell.setCellValue(title);
            cell.setCellStyle(cellStyle1);
        }
        //合并单元格
        CellRangeAddress region1 =new CellRangeAddress(0, 0, 0, 6);
        sheet.addMergedRegion(region1);
        AtomicInteger ai1 = new AtomicInteger(1);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Cell cell = null;
        Row row2 = sheet.createRow(ai1.getAndIncrement());
        cell = row2.createCell(0);
        cell.setCellValue("Date");
        //设置样式
        cell.setCellStyle(cellStyle);
        cell = row2.createCell(1);
        cell.setCellValue(productionTime);
        //设置样式
        cell.setCellStyle(cellStyle);
        sheet.setColumnWidth(1, 12 * 256);//设置第一列的宽度是31个字符宽度
        sheet.setColumnWidth(12, 30 * 256);//设置第一列的宽度是31个字符宽度
        row2.setHeightInPoints(22);//设置行的高度是50个点
        AtomicInteger ai2 = new AtomicInteger(2);
        Row row3 = sheet.createRow(ai2.getAndIncrement());
        cell = row3.createCell(0);
        cell.setCellValue("Shift");
        //设置样式
        cell.setCellStyle(cellStyle);
        cell = row3.createCell(1);
        cell.setCellValue(shift);
        //设置样式
        cell.setCellStyle(cellStyle);
        //合并单元格
        CellRangeAddress region2 =new CellRangeAddress(1, 2, 2, 6);
        sheet.addMergedRegion(region2);
        // 合并单元格上边框样式
//        AtomicInteger ai3 = new AtomicInteger(3);
//        Row row4 = sheet.createRow(ai3.getAndIncrement());
//        cell = row4.createCell(0);
//        cell.setCellValue("");
//        //设置样式
//        cell.setCellStyle(cellStyle);
//
//        cell = row4.createCell(2);
//        cell.setCellValue("检查通过台数1");
//        //设置样式
//        cell.setCellStyle(cellStyle);
//        CellRangeAddress region3 =new CellRangeAddress(3, 3, 0, 1);
//        //合并单元格
//        sheet.addMergedRegion(region3);
//        CellRangeAddress region4 =new CellRangeAddress(3, 3, 2, 12);
//        //合并单元格
//        sheet.addMergedRegion(region4);

      //3.构造sheet
        String[] titles = {"编号", "时间带", "直行率","纯直行率", "再涂装率","废品率","综合效率"};
        Row row5 =sheet.createRow(3);
        AtomicInteger ai3 = new AtomicInteger();
        for (String title : titles) {
            cell = row5.createCell(ai3.getAndIncrement());
            cell.setCellValue(title);
            //设置样式
            cell.setCellStyle(cellStyle);
        }
        AtomicInteger datasAi = new AtomicInteger(4);
        int i = 1;
        Integer straightRate = 0;//直行率
        Integer rateOfPureStraight = 0;//纯直行率
        Integer theCoatingRateAgain = 0;//再涂装率
        Integer rejectionRate = 0;//废品率
        Integer coatingEfficiency = 0;//涂合效率
        if(list.size()>0){
            for (CheckTheNumberOfUnits2 report : list) {
                straightRate = straightRate+report.getStraightRate();
                rateOfPureStraight = rateOfPureStraight+report.getRateOfPureStraight();
                theCoatingRateAgain = theCoatingRateAgain + report.getTheCoatingRateAgain();
                rejectionRate = rejectionRate + report.getRejectionRate();
                coatingEfficiency = coatingEfficiency+report.getCoatingEfficiency();

                Row dataRow = sheet.createRow(datasAi.getAndIncrement());
                //编号
                cell = dataRow.createCell(0);
                cell.setCellValue(i++);
                cell.setCellStyle(cellStyle);
                //时间带
                cell = dataRow.createCell(1);
                cell.setCellValue(report.getTimeZone());
                cell.setCellStyle(cellStyle);
                //直行率
                cell = dataRow.createCell(2);
                cell.setCellValue(report.getStraightRate());
                cell.setCellStyle(cellStyle);
                //纯直行率
                cell = dataRow.createCell(3);
                cell.setCellValue(report.getRateOfPureStraight());
                cell.setCellStyle(cellStyle);
                //再涂装率
                cell = dataRow.createCell(4);
                cell.setCellValue(report.getTheCoatingRateAgain());
                cell.setCellStyle(cellStyle);
                //废品率
                cell = dataRow.createCell(5);
                cell.setCellValue(report.getRejectionRate());
                cell.setCellStyle(cellStyle);
                //涂合效率
                cell = dataRow.createCell(6);
                cell.setCellValue(report.getCoatingEfficiency());
                cell.setCellStyle(cellStyle);
            }
            AtomicInteger ai6 = new AtomicInteger(list.size()+4);
            Row row6 = sheet.createRow(ai6.getAndIncrement());
            cell = row6.createCell(0);
            cell.setCellValue(" TOTAL");
            cell.setCellStyle(cellStyle);
            cell = row6.createCell(1);
            cell.setCellValue(0);
            cell.setCellStyle(cellStyle);
            cell = row6.createCell(2);
            cell.setCellValue(straightRate);
            cell.setCellStyle(cellStyle);
            cell = row6.createCell(3);
            cell.setCellValue(rateOfPureStraight);
            cell.setCellStyle(cellStyle);
            cell = row6.createCell(4);
            cell.setCellValue(theCoatingRateAgain);
            cell.setCellStyle(cellStyle);
            cell = row6.createCell(5);
            cell.setCellValue(rejectionRate);
            cell.setCellStyle(cellStyle);
            cell = row6.createCell(6);
            cell.setCellValue(coatingEfficiency);
            cell.setCellStyle(cellStyle);
            CellRangeAddress region8 =new CellRangeAddress(list.size()+4, list.size()+4, 0, 1);
            //合并单元格
            sheet.addMergedRegion(region8);
        }

        String fileName = URLEncoder.encode(sheet.getSheetName()+".xlsx", "UTF-8");
        response.setContentType("application/octet-stream");
        response.setHeader("content-disposition", "attachment;filename=" + new
                String(fileName.getBytes("ISO8859-1")));
        response.setHeader("filename", fileName);
        workbook.write(response.getOutputStream());
    }
    //合并单元格样式
    private void setCellBorder(Sheet sheet, CellRangeAddress cellAddresses) {
        RegionUtil.setBorderTop(BorderStyle.MEDIUM, cellAddresses, sheet);
        RegionUtil.setBorderBottom(BorderStyle.MEDIUM, cellAddresses, sheet);
        RegionUtil.setBorderLeft(BorderStyle.MEDIUM, cellAddresses, sheet);
        RegionUtil.setBorderRight(BorderStyle.MEDIUM, cellAddresses, sheet);


    }
}
